using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support;
using Sedna.Core.DataBinding;
using Sedna.Core.Tiles.Contents;

namespace Sedna.Core.Controls
{
    public class FileUpload : System.Web.UI.WebControls.FileUpload, IContent, IConfigurationAware
    {
        #region Fields
        
        private string configurationId = "configuration.default.FileUpload";
        private IConfiguration configuration;
        private string contentPlaceHolderId;
        protected IDataBinder binder;

        #endregion

        #region Properties
        
        public string ContentPlaceHolderId
        {
            get { return contentPlaceHolderId; }
            set { contentPlaceHolderId = value; }
        }

        [Bindable(true), Category("Sedna"), DefaultValue("configuration.default.FileUpload")]
        public IConfiguration Configuration
        {
            get { return configuration; }
            set { configuration = value; }
        }

        public string ConfigurationId
        {
            get { return configurationId; }
            set { configurationId = value; }
        }
        
        #endregion

        #region Constructors
        
        public FileUpload() :base()
		{
			Width=Unit.Pixel(120);
		}

		public FileUpload(IConfiguration configuration): this()
		{
			Configuration = configuration;
			ConfigurationId = configuration.ObjectName;
			BuildControlFromConfiguration();
		}

        public FileUpload(string id)
            : this()
		{
			this.ID = id;
        }

        #endregion

        #region OnXXX

        protected override void OnInit(EventArgs e)
        {
            if (configuration == null)
            {
                configuration = (IConfiguration)SpringContextFactory.GetContext().GetObject(configurationId);
            }
            Build();
            binder = new DataBinder(this, configuration, ExecutionContext.GetCurrent(ClientID));
            base.OnInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            IExecutionContext executionContext = ExecutionContext.GetCurrent(ClientID);
            ControlUtil.ExecuteOnPreRender(executionContext, Context, Configuration, this.ClientID);
            Bind();

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            IExecutionContext executionContext = ExecutionContext.GetCurrent(ClientID);
            ControlUtil.ExecuteOnLoad(executionContext, Context, Configuration, this.ClientID);

            Unbind();								

            if (Configuration.VisibleBindPath != null)
                this.Visible = true; // To reach the OnPreRender method and analyze visibility in the Bind method
        }

        public void OnDataLoad()
        {
            IDictionary executionContext = ExecutionContext.GetCurrent(ClientID);
            ControlUtil.ExecuteOnDataLoad(executionContext, Context, Configuration, this.ClientID);
        }

        #endregion
        
        #region Build

        protected void BuildControlFromConfiguration()
        {
            ID = configuration.Id;
            Width = configuration.Width;
            Height = configuration.Height;

            IFileUploadConfiguration fileUploadConfiguration = configuration as IFileUploadConfiguration;
            if (fileUploadConfiguration == null)
            {
                return;
            }
        }

        protected void Build()
        {
            Enabled = configuration.IsEnabledForPrincipal() && configuration.Enabled;
            Visible = configuration.IsVisibleForPrincipal();
        }
        #endregion

        #region Bind/Unbind
        
        protected virtual void Bind()
        {
            Visible = Configuration.Visible;

            binder.Bind("Enabled");
            binder.Bind("Visible");

            Enabled = Enabled && Configuration.IsEnabledForPrincipal();
            Visible = Visible && Configuration.IsVisibleForPrincipal();
            
            if (Enabled || Visible)
            {
                IExecutionContext executionContext = ExecutionContext.GetCurrent(ClientID);
                IFileUploadConfiguration fileUploadConfiguration = configuration as IFileUploadConfiguration;

                IFileUploadCommand command =
                    (IFileUploadCommand)executionContext.GetPropertyValue(fileUploadConfiguration.SaveAsCommandPath);
                
                if (command != null && HasFile)
                {
                    command.Execute(this);
                }
            }
        }

        protected virtual void Unbind()
        {
            binder.Unbind("Enabled");
            binder.Unbind("Visible");
            binder.Unbind("HasFile");
            binder.Unbind("FileName");
        }

        #endregion
    }
}
